package controlLayer;

import java.sql.SQLException;
import java.util.List;

import utilLayer.AlreadyExistsException;
import dbLayer.DBConnection;
import modelLayer.*;
import dbLayer.*;

/**
 * This class is a control layer class of the MSAS system. It functions as the link between 
 * our database and the interface. This class is limited to working with the Teacher class,
 * and whichever other classes teacher is associated with. There is no direct connection between
 * this class and our database.
 * 
 * @author Danny M�lgaard, Anna Kelemen, Jakob Korsgaard
 * 		   
 * @version 1.3.1
 * 					1.3.1: 		Added more extensive documentation. Verified that
 * 								information contained in the Design Class Diagram,
 * 								is in accordance with the information contained in this
 * 								class.
 *					1.3.2:		Try&catch and throws Exception added to the constructor in order to catch
 *								a NullPointerException and to avoid the program from crashing
 *								if there is no connection to the database.
 */
public class TeacherCtr {
	
	private DBTeacher dbTeacher;
	
	/**
	 * Creates an instance of TeacherCtr. Initializes an instance of DBTeacher.
	 * @throws Exception
	 */
	public TeacherCtr() throws Exception {
		try {
			dbTeacher = new DBTeacher();
		}
		catch (Exception e) {
			throw new Exception();
		}

	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the insertion of a new row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in dataloss,
	 * or unintended data manipulation occur.
	 * 
	 * @param	name	String	name of the teacher
	 * @param	cpr		String	ssn of the teacher
	 * @param	address	String	address of the teacher
	 * @param	phone	String	phonenumber of the teacher
	 * @param	email	String	email of the teacher
	 * @param	hours	int		number of hours per week the teacher works
	 * @return 	rc 		int 		count of rows affected by the query execution in the database		
	 * @throws 		Exception
	 */
	public int createTeacher(String name, String cpr, String address, String phone,
							 String email, int hours) throws Exception {
				
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = this.dbTeacher.insert(new Teacher(name, cpr, address, phone, email, hours));
			DBConnection.commitTransaction();			
		}
		catch (SQLException ex) {
			DBConnection.rollbackTransaction();
			throw new AlreadyExistsException(name);
		}
		catch(Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: createTeacher(): " + e);
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	name		String			teacher name.
	 * @throws				Exception		throws SQL exceptions if the query isn't
	 * 										legal.
	 * @return				List<Teacher>	returns the teachers matching the search.
	 */
	public List<Teacher> findTeacher(String name) throws Exception {
		
		List<Teacher> list = null;
		try {
			list = this.dbTeacher.find(name);
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: findTeacher(): " + e);
		}
		
		return list;

	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	id			int				teacher id.
	 * @throws				Exception		throws SQL exceptions if the query isn't
	 * 										legal.
	 * @return				List<Teacher>	returns the teachers matching the search.
	 */
	public Teacher findTeacherById(int id) {
		
		Teacher t = null;
		try {
			t = this.dbTeacher.findById(id);
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: findTeacherById(): " + e);
		}
		
		return t;

	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of all existing rows in the database.
	 * 
	 * @throws				Exception		throws SQL exceptions if the query isn't
	 * 										legal.
	 * @return				List<Teacher>	returns a list of all teachers.
	 */
	public List<Teacher> getAll() {
		
		List<Teacher> list = null;
		try {
			list = this.dbTeacher.getAll();
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: getAll(): " + e);
		}
		
		return list;
		
	}

	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the renewal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param 	t	Teacher		the teacher object to be updated in the database
	 * @return 	rc 	int 		count of rows affected by the query execution in the database		
	 * @throws 		Exception
	 */
	public int updateTeacher(Teacher t) throws Exception {
		
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = this.dbTeacher.update(t.getId(), t.getName(), t.getCpr(), t.getAddress(), t.getPhone(), t.getEmail(), t.getHours());
			DBConnection.commitTransaction();
		}
		catch (SQLException ex) {
			DBConnection.rollbackTransaction();
			throw new AlreadyExistsException(t.getName());
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: updateTeacher(): " + e);
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the removal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param 	id			int			id of the teacher
	 * @return	rc			int			count of rows affected by the query execution in the database
	 * @throws 				Exception
	 */
	public int removeTeacher(int id) throws Exception {
		
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = this.dbTeacher.remove(id);
			DBConnection.commitTransaction();
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [TEACHERCTR]: removeTeacher(): " + e);
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	

	
}
